package com.platform.hjt.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.costant.UserType;
import com.platform.hjt.sys.model.SysCompanyInfo;
import com.platform.hjt.sys.model.SysDept;
import com.platform.hjt.sys.model.SysModel;
import com.platform.hjt.sys.model.SysUserInfo;
import com.platform.hjt.sys.service.SysCompanyInfoService;
import com.platform.hjt.sys.service.SysDeptService;
import com.platform.hjt.sys.service.SysModelService;
import com.platform.hjt.sys.service.SysUserInfoService;

@Controller
@RequestMapping(value="/sysUserInfo")
public class SysUserInfoController extends BaseController{

	@Resource
	private SysUserInfoService sysUserInfoService;
	@Resource
	private SysCompanyInfoService sysCompanyInfoService;
	@Resource
	private SysDeptService sysDeptService;
	@Resource
	private SysModelService sysModelService;
	
	@RequestMapping(value="/sysUserInfo_list.html")
	public String sysUserInfoListPage(){
		return "page/admin/sysUserInfo/sysUserInfo_list";
	}
	
	@RequestMapping(value="/sysUserInfo_list.do")
	@ResponseBody
	public Page<SysUserInfo> sysUserInfoListDo(Page<SysUserInfo>page){
		
		List<PropertyFilter>  propertyFilterList = PropertyFilter.buildFromHttpRequest(getRequest());
		propertyFilterList.add(new PropertyFilter("EQB_isDel", "false"));
		//非管理员人只能获取本公司的人员
		if(!getSessionUser().getUserType().equals(UserType.MASTER.getKey())){
			propertyFilterList.add(new PropertyFilter("EQS_sysCompanyInfo.sysCompanyInfoId", getSessionSysCompanyInfo().getSysCompanyInfoId()));
		}
		
		return sysUserInfoService.getSysUserInfoList(page, propertyFilterList);
	}
	
	@RequestMapping(value="/sysUserInfo_edit.html")
	public String sysUserInfoEditPage(HttpServletRequest request, Model model, String id){
		
		if(StringUtils.isNotBlank(id)){
			SysUserInfo user = sysUserInfoService.getSysUserInfo("sysUserInfoId", id);
			List<SysModel> modelList = sysModelService.getUserModel(id);
			String sysModelIds = "";
			for(SysModel scm : modelList) {
				sysModelIds += scm.getSysModelId() + ",";
			}
			if(StringUtils.endsWith(sysModelIds, ",")){
				sysModelIds = StringUtils.substringBeforeLast(sysModelIds, ",");
			}
			user.setSysModelIds(sysModelIds);
			model.addAttribute("sysUserInfo", user);
		}
		
		return "page/admin/sysUserInfo/sysUserInfo_edit";
	}
	
	@RequestMapping(value="/sysUserInfo_detail_{id}.html")
	public String sysUserInfoDetailPage(Model model, @PathVariable String id){
		
		SysUserInfo user = sysUserInfoService.getSysUserInfo("sysUserInfoId", id);
		model.addAttribute("sysUserInfo", user);
		
		return "page/admin/sysUserInfo/sysUserInfo_detail";
	}
	
	@RequestMapping(value="/sysUserInfo_edit.do")
	@ResponseBody
	public ReturnBody sysUserInfoEditDo(@Valid @ModelAttribute("sysUserInfo") SysUserInfo sysUserInfo, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		return sysUserInfoService.saveSysUserInfo(sysUserInfo);
	}
	
	@RequestMapping(value="/sysUserInfo_del_{id}.do")
	@ResponseBody
	public ReturnBody sysUserInfoDelDo(@PathVariable String id){
		return sysUserInfoService.delSysUserInfo(new SysUserInfo(id));
	}

	@RequestMapping(value="/getUserCompanyTreeJson.do")
	@ResponseBody
	public JSONObject getUserCompanyTreeJson(){
		JSONObject json = new JSONObject();
		
		JSONArray arr = new JSONArray();
		List<String> ids = new ArrayList<String>();
		//非管理员只能获取本公司的
		if(!getSessionUser().getUserType().equals(UserType.MASTER.getKey())){
			JSONObject jTemp = new JSONObject();
			jTemp.put("parentId", "0");
			jTemp.put("id",getSessionSysCompanyInfo().getSysCompanyInfoId());
			jTemp.put("name", getSessionSysCompanyInfo().getCompanyName());
			jTemp.put("icon", "../QUI/libs/icons/user_group.gif");
			jTemp.put("flag", "0");
			arr.add(jTemp);
			ids.add(getSessionSysCompanyInfo().getSysCompanyInfoId());
			//ids = getSysCompanyInfo().getSysCompanyInfoId();
		} else{
			//管理员能查看到所有
			List<SysCompanyInfo>list = sysCompanyInfoService.getAll();
			for(SysCompanyInfo sci : list){
				JSONObject jTemp = new JSONObject();
				jTemp.put("parentId", "0");
				jTemp.put("id", sci.getSysCompanyInfoId());
				jTemp.put("name", sci.getCompanyName());
				jTemp.put("icon", "../QUI/libs/icons/user_group.gif");
				jTemp.put("flag", "0");
				arr.add(jTemp);
				ids.add(sci.getSysCompanyInfoId());
				//ids += "," + sci.getSysCompanyInfoId();
			}
		}
		List<SysDept> sysDeptList = sysDeptService.getSysDeptListByCompanyIds(ids);
		for(SysDept sd : sysDeptList){
			JSONObject jTemp = new JSONObject();
			jTemp.put("parentId", sd.getSysDept()!=null?sd.getSysDept().getSysDeptId():sd.getSysCompanyInfo().getSysCompanyInfoId());
			jTemp.put("id", sd.getSysDeptId());
			jTemp.put("name", sd.getDeptName());
			jTemp.put("icon", "../QUI/libs/icons/user_group.gif");
			jTemp.put("flag", "1");
			jTemp.put("companyId", sd.getSysCompanyInfo().getSysCompanyInfoId());
			arr.add(jTemp);
		}
		json.put("list", arr);
		return json;
	}
	
	/**
	 * 获取本公司的部门/人员树
	 * @Description 
	 * @author huangjt
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/getCompanyUsersTreeJson.do")
	@ResponseBody
	public JSONObject getCompanyUsersTreeJson(){
		JSONObject json = new JSONObject();
		JSONArray arr = new JSONArray();
		List<String> ids = new ArrayList<String>();
		
		
		//只能获取本公司的
		ids.add(getSessionSysCompanyInfo().getSysCompanyInfoId());
		List<SysDept> sysDeptList = sysDeptService.getSysDeptListByCompanyIds(ids);
		
		List<String> deptIds = new ArrayList<String>();
		for(SysDept sd : sysDeptList){
			deptIds.add(sd.getSysDeptId());
			JSONObject jTemp = new JSONObject();
			jTemp.put("parentId", sd.getSysDept()!=null?sd.getSysDept().getSysDeptId():sd.getSysCompanyInfo().getSysCompanyInfoId());
			jTemp.put("id", sd.getSysDeptId());
			jTemp.put("name", sd.getDeptName());
			//部门不可被选中
			jTemp.put("clickExpand", true);
			jTemp.put("icon", "../QUI/libs/icons/user_group.gif");
			arr.add(jTemp);
		}
		
		List<SysUserInfo> userList = sysUserInfoService.getUserInfoListBySysDeptIds(deptIds);
		for(SysUserInfo user : userList){
			JSONObject jTemp = new JSONObject();
			jTemp.put("parentId", user.getSysDept()!=null?user.getSysDept().getSysDeptId():"-1");
			jTemp.put("id", user.getSysUserInfoId());
			jTemp.put("name", user.getUserName());
			jTemp.put("icon", "../QUI/libs/icons/user-green.png");
			arr.add(jTemp);
		}
		
		json.put("treeNodes", arr);
		return json;
	}
	
	@RequestMapping(value="/dialogSysUserInfoSelect.html")
	public String dialogSysUserInfoSelect(Model model){
		return "page/admin/sysUserInfo/dialog_sysUserInfo_select";
	}
	
	
}
